$(function () {
    // 添加全局AJAX请求拦截
    $.ajaxSetup({
        beforeSend: function (xhr) {
            // 从sessionStorage获取token
            let token = sessionStorage.getItem('token');
            if (token) {
                // 设置Authorization请求头
                xhr.setRequestHeader('Authorization', token);
            }
        },
        complete: function (xhr, status) {
            // 如果返回401未授权，跳转到登录页
            if (xhr.status === 401) {
                sessionStorage.removeItem('token');
                location.href = '/login.html';
            }
        }
    });
});